﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CompanyManagementDLL.MetaData;

namespace CompanyManagementDLL.DLL
{
    public class AttachmentDLL
    {
        private readonly CompanyMgtEntities db = new CompanyMgtEntities();

        #region Fetch Data

        /// <summary>
        /// Return attachment detail based on id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AttachmentMetaData GetAttachmentDetailID(int id)
        {
            AttachmentMetaData attcahmentmetadata = new AttachmentMetaData();
            try
            {
                attcahmentmetadata = db.Attachments.Where(a => a.AttachmentID == id).Select(a => new AttachmentMetaData
                {
                    AttachmentID = a.AttachmentID,
                    Path = a.Path,
                    TaskID = a.TaskID,
                    AttachmentName = a.AttachmentName,
                    UploadedBy=a.UploadedBy,
                    Detail=a.Detail
                }).FirstOrDefault();
            }
            catch (Exception ex)
            {

                throw ex;
            }

            return attcahmentmetadata;

        }
        #endregion

        #region Get Attachment List

        /// <summary>
        /// Return attachment list based on task id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<AttachmentMetaData> GetAttachmentListByTaskID(int id)
        {
            List<AttachmentMetaData> attcahmentmetadata = new List<AttachmentMetaData>();
            try
            {
                attcahmentmetadata = db.Attachments.Where(a => a.TaskID == id).Select(a => new AttachmentMetaData
                {
                    AttachmentID = a.AttachmentID,
                    Path = a.Path,
                    TaskID = a.TaskID,
                    AttachmentName = a.AttachmentName,
                    UploadedBy = a.UploadedBy,
                    Detail = a.Detail
                }).ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return attcahmentmetadata;

        }

        #endregion

        #region DML Operation

        /// <summary>
        /// Add attchment detail in table
        /// </summary>
        /// <param name="attachmentmetadata"></param>
        public void AddAttachment(AttachmentMetaData attachmentmetadata)
        {
            Attachment tblAttach = new Attachment();
            try
            {
                tblAttach.Path = attachmentmetadata.Path;
                tblAttach.TaskID = attachmentmetadata.TaskID;
                tblAttach.AttachmentName = attachmentmetadata.AttachmentName;
                tblAttach.UploadedBy = attachmentmetadata.UploadedBy;
                tblAttach.Detail = attachmentmetadata.Detail;
                db.Attachments.Add(tblAttach);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        /// <summary>
        /// Delete attachment from table
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAttachment(int id)
        {
            try
            {
                var attachment = db.Attachments.Where(a => a.AttachmentID == id).FirstOrDefault();
                if (attachment != null)
                {
                    db.Attachments.Remove(attachment);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        #endregion
    
    }
}
